<?php

namespace Api\Member\AuthManager\Open;

use Illuminate\Http\Request;
use Laravel\Lumen\Routing\Controller as BaseController;

/**
 * 开放权限控制器
 */
class Controller extends BaseController
{
    /**
     * 构造方法
     */
    public function __construct(
        ManagerRepository      $ManagerRepository,
        \Api\Sms\SmsRepository $SmsRepository
    ) {
        $this->ManagerRepository = $ManagerRepository;
        $this->SmsRepository     = $SmsRepository;
    }

    /**
     * 重置密码
     *
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function resetPassword(Request $request)
    {
        try {
            $account          = $request->get('account');
            $mobile           = $request->get('mobile');
            $countryCode      = $request->get('country_code');
            $verificationCode = $request->get('verification_code');
            $nowPassword      = $request->get('password');
            
            $this->SmsRepository->checkVerificationCode($verificationCode, self::class.'@resetPassword', $mobile, $countryCode);
            $this->ManagerRepository->resetPassword($nowPassword, $account, $mobile, $countryCode);
            _success();
        } catch (\Sms\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }

    /**
     * 发送验证码（重置密码）
     *
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function sendResetPasswordVerificationCode(Request $request)
    {
        try {
            $account     = $request->get('account');
            $mobile      = $request->get('mobile');
            $countryCode = $request->get('country_code');
            
            $this->ManagerRepository->checkAccountMobile($account, $mobile, $countryCode);
            $this->SmsRepository->sendVerificationCode(self::class.'@resetPassword', $mobile, $countryCode);
            _success();
        } catch (\Sms\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }

    /**
     * 验证并获取 token
     * 
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function login(Request $request)
    {
        try {
            $account          = $request->get('account');
            $mobile           = $request->get('mobile');
            $countryCode      = $request->get('country_code');
            $verificationCode = $request->get('verification_code');
            $this->ManagerRepository->checkAccountMobile($account, $mobile, $countryCode);
            $this->SmsRepository->checkVerificationCode($verificationCode, self::class.'@login', $mobile, $countryCode);
            $token = $this->ManagerRepository->login($account);
            _success(compact('token'));
        } catch (\Sms\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }

    /**
     * 发送验证码（登录）
     *
     * @param \Illuminate\Http\Request $request
     * @return void
     */
    public function sendLoginVerificationCode(Request $request)
    {
        try {
            $account     = $request->get('account');
            $mobile      = $request->get('mobile');
            $countryCode = $request->get('country_code');
            
            $this->ManagerRepository->checkAccountMobile($account, $mobile, $countryCode);
            $this->SmsRepository->sendVerificationCode(self::class.'@login', $mobile, $countryCode);
            _success();
        } catch (\Sms\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        } catch (\Member\AuthManager\Exceptions\Exception $e) {
            _error('validation_fail', $e->getMessage());
        }
    }

    /**
     * 刷新当前 token
     *
     * @return void
     */
    public function refreshToken()
    {
        try {
            $token = $this->ManagerRepository->refreshToken();
            _success(compact('token'));
        } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
            _error('unauthorized');
        }
    }

    /**
     * 登出，注销当前 token
     *
     * @return void
     */
    public function logout()
    {
        try {
            $this->ManagerRepository->logout();
            _success();
        } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {
            _error('unauthorized');
        }
    }


}
